﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enring.Model.Global;
using Enring;
using Enring.Data;
using System.Data;
using System.Data.SqlClient;



namespace Enring.Model.Range
{
    public enum Gender{
        Male = 1,
        Female = 0
    }

    public partial class GenderWrapper {
        private Gender gender;
        public int Value {
            get {
                return (int)gender;
            }
            set {
                gender = (Gender)value;
            }
        }
        public Gender EnumValue {
            get { return gender; }
            set { gender = value; }
        }
        public static implicit operator GenderWrapper(Gender g) {
            return new GenderWrapper() { EnumValue = g };
        }
        public static implicit operator Gender(GenderWrapper gw) {
            if (gw == null)
                return Gender.Male;
            return gw.EnumValue;
        }

    }

    public class User 
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Password { get; set; }
        public Gender Gender { get; set; }
        public DateTime CreatedTime { get; set; }
        public DateTime LastLoginTime { get; set; }
        public Language DefaultLanguage { get; set; }
        public Ring ParentRing { get; set; }

        public static User FindFirst(string uid)
        {            
            string sql = "SELECT * FROM SYS_Users WHERE Name = @user";
            List<SqlParameter> parameters = new List<SqlParameter>();
            SqlParameter paramUser = new SqlParameter("user", SqlDbType.NVarChar);
            paramUser.Value = uid;

            parameters.Add(paramUser);
            
            DataSet dsUser = DB.GetDataSet(DB.ConnectionStringFoundation, System.Data.CommandType.Text, sql, parameters.ToArray());

            if (DB.HasData(dsUser))
            {
                DataRow row = dsUser.Tables[0].Rows[0];
                User u = new User();
                u.ID = row["ID"].ToString();
                u.Name = row["Name"].ToString();
                u.CreatedTime = Convert.ToDateTime(row["CreatedTime"]);
                u.LastLoginTime = Convert.ToDateTime(row["LastLoginTime"]);
                return u;
            }
            else
                return null;
        }

        /// <summary>
        /// Check the user authentication
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool CheckLogin(string uid, string password)
        {
            string sql = "SELECT COUNT(*) FROM SYS_Users WHERE Name = @user AND Password = @password";
            List<SqlParameter> parameters = new List<SqlParameter>();
            SqlParameter paramUser = new SqlParameter("user",SqlDbType.NVarChar);
            paramUser.Value = uid;

            SqlParameter paramPassword = new SqlParameter("password", SqlDbType.NVarChar);
            paramPassword.Value = password;

            parameters.Add(paramUser);
            parameters.Add(paramPassword);

            int count = (int)DB.ExecuteScalar(DB.ConnectionStringFoundation, System.Data.CommandType.Text, sql, parameters.ToArray());
            return count > 0;
        }

    }
}
